const jwt = require("jsonwebtoken");

const jwtKey = "junkaicool"; // token生成的密匙，根据自己需求定义

const jwtSign = (data) => {
  // token生成函数，有效时间为一个小时
  const token = jwt.sign(data, jwtKey, { expiresIn: 60 * 60 * 24 });
  return token;
};

const jwtCheck = (token) => {
  return new Promise((resolve, reject) => {
    jwt.verify(token, jwtKey, (err, data) => {
      if (err) {
        console.log('err',err)
        reject(err);
      } else {
        console.log('data',data)
        resolve(data);
      }
    });
  });
};

module.exports = {
  jwtSign,
  jwtCheck,
};
